package falcon.script;

import falcon.common.Errno;

public final class FalconLangScript {

	public FalconLangScript(String path, String... args) {
		m_path = path;
		m_global = new FalconLangGlobal();
		fillArgs(args);
	}
	
	public FalconLangScript(FalconLangGlobal global, String name, String... args) {
		m_path = global.getFlow(name);
		m_global = global;
		fillArgs(args);
	}
	
	public int execute() {
		FalconLangProcessor processor = m_global.getProcessor();
		int errno = processor.load(m_path, m_args);
		if (errno != Errno.SUCCESS) {
			return errno;
		}
		errno = processor.execute();
		return errno;
	}
	
	public int printAssembly(StringBuilder sb) {
		FalconLangDebuger debuger = m_global.getDebuger();
		return debuger.printAssembly(m_path, sb);
	}
	
	private void fillArgs(String... args) {
		m_args = new String[16];
		for (int i = 0; i < m_args.length; i ++) {
			m_args[i] = "";
		}
		if (args == null || args.length < 1) {
			return;
		}
		int count = Math.min(args.length, m_args.length);
		for (int i = 0; i < count; i ++) {
			m_args[i] = args[i];
		}
	}
	
	private String m_path;
	private FalconLangGlobal m_global;
	private String[] m_args;
}
